/******************************************************************************
* project: automatic_alarm_free_rtos_based
* chip: STM32F407VG
* compiler: arm-none-eabi-gcc (GNU Tools for ARM Embedded Processors) 4.6.2
* 	20110921 (release) [ARM/embedded-4_6-branch revision 182083]
*
* prefix: (none)
*
* available global functions:
* 	spi_init(void)
* 	spi_write(uint32_t regdata)
* 	spi_read(uint32_t *regdata)
*
*
* available local functions:
*
*
* available interrupt handlers:
*
* Created on: Mar 15, 2013
*      Author: podol
*
******************************************************************************/

#include "stm_usart.h"
#include "config.h"
#include "gpio.h"

/*------------------------------------------------------------------------*//**
* \brief Configures and initialize the USART1.
* \details
*
* \param [in] void
*//*-------------------------------------------------------------------------*/
void usart1_init(uint32_t BAUD, uint32_t apbclock)
{
	uint32_t tmpreg = 0x00;
	uint32_t integerdivider = 0x00;
	uint32_t fractionaldivider = 0x00;

	integerdivider = ((25 * apbclock) / (2 * (BAUD)));
	tmpreg = (integerdivider / 100) << 4;
	fractionaldivider = integerdivider - (100 * (tmpreg >> 4));
	tmpreg |= ((((fractionaldivider * 8) + 50) / 100)) & ((uint8_t)0x07);

	RCC->APB2ENR |= RCC_APB2ENR_USART1EN;		//Enable USART1 Clock
	gpio_pin_cfg(USART1_GPIO, USART1_TX_pin, GPIO_AF7_PP_100MHz);
	gpio_pin_cfg(USART1_GPIO, USART1_RX_pin, GPIO_AF7_PP_100MHz);

	USART2->BRR = (uint16_t)tmpreg;
	USART2->CR1 |=   USART_CR1_TE |  USART_CR1_RE | USART_CR1_UE;  // RX, TX enable
}


/*------------------------------------------------------------------------*//**
* \brief Configures and initialize the USART1.
* \details
*
* \param [in] void
*//*-------------------------------------------------------------------------*/
void usart2_init(uint32_t BAUD, uint32_t apbclock)
{
	uint32_t tmpreg = 0x00;
	uint32_t integerdivider = 0x00;
	uint32_t fractionaldivider = 0x00;

	integerdivider = ((25 * apbclock) / (2 * (BAUD)));
	tmpreg = (integerdivider / 100) << 4;
	fractionaldivider = integerdivider - (100 * (tmpreg >> 4));
	tmpreg |= ((((fractionaldivider * 8) + 50) / 100)) & ((uint8_t)0x07);

	RCC->APB1ENR |= RCC_APB1ENR_USART2EN; 		//Enable USART2 Clock
	gpio_pin_cfg(USART2_GPIO, USART2_TX_pin, GPIO_AF7_PP_100MHz);
	gpio_pin_cfg(USART2_GPIO, USART2_RX_pin, GPIO_AF7_PP_100MHz);

	USART2->BRR = tmpreg;
	USART2->CR2 |= 1 << 1;
	USART2->CR1 |=   USART_CR1_TE |  USART_CR1_RE | USART_CR1_UE;  // RX, TX enable
}

/*------------------------------------------------------------------------*//**
* \brief Put a char to USARTx.
* \details
*
* \param [in] void
*//*-------------------------------------------------------------------------*/
void usart_putchar(USART_TypeDef* myUSART, uint32_t data)
{
	myUSART->DR = (char)data;
	while(((myUSART->SR >> 6) & 0x01) == 0);
}

/*------------------------------------------------------------------------*//**
* \brief Put a string to USARTx.
* \details
*
* \param [in] void
*//*-------------------------------------------------------------------------*/
void usart_putstr(USART_TypeDef* myUSART, char *data)
{
	while(*data)
		usart_putchar(myUSART,*data++);
}
